<?php

namespace common\models\book;

use Yii;
use common\helpers\StringHelper;

/**
 * This is the model class for table "{{%book_staff}}".
 *
 * @property int $id
 * @property int $book_id 预约ID
 * @property string $realname 姓名
 * @property string|null $mobile 手机号
 * @property string|null $position 职务/职位
 * @property int $company_id 单位ID
 * @property string $company_name 单位
 * @property int $recorder_id 录入人ID
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class BookStaff extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%book_staff}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['book_id', 'company_id', 'recorder_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['realname', 'company_name'], 'required'],
            [['realname', 'position'], 'string', 'max' => 50],
            [['mobile'], 'string', 'max' => 11],
            [['company_name'], 'string', 'max' => 100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'book_id' => '预约ID',
            'realname' => '姓名',
            'mobile' => '手机号',
            'position' => '职务/职位',
            'company_id' => '单位ID',
            'company_name' => '单位',
            'recorder_id' => '录入人ID',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getBook() {
        return $this->hasOne(Book::class, ['id' => 'book_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getCompany() {
        return $this->hasOne(Company::class, ['id' => 'company_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getMaskMobile() {
        return $this->mobile ? StringHelper::maskChar($this->mobile) : '-';
    }

    /**
     * {@inheritdoc}
     */
    public function getCanDelete() {
        if ($this->book && $this->book->status < 10 && $this->book->status >= 0) {
            return true;
        }
        return false;
    }

}
